import { orderList, searchOrder } from '@/api/order'
export default {
  namespaced: true,
  state () {
    return {
      orderTotal: '', // 订单总数
      orderIdList: [], // 订单id列表
      orderDetailsList: [] // 订单详情列表
    }
  },
  getters: {

  },
  mutations: {
    getOrderTotal (state, total) {
      state.orderTotal = total
    },
    getOrderIdList (state, ordersId) {
      state.orderIdList = ordersId
    },
    getOrderDetailsList (state, orderDetails) {
      state.orderDetailsList = orderDetails
    }

  },
  actions: {
    async getOrderListAction (context) {
      const { records, total } = await orderList()
      records.sort(function (a, b) {
        const value1 = a.status
        const value2 = b.status
        return value1.localeCompare(value2)
      })
      const ordersId = records.map((item) => {
        return item.orderId
      })
      context.commit('getOrderTotal', total)
      context.commit('getOrderIdList', ordersId)
      context.dispatch('getOrderDetailsList', ordersId)
    },
    async getOrderDetailsList (context, ordersId) {
      const arr = []
      for (let index = 0; index < ordersId.length; index++) {
        const res = await searchOrder(ordersId[index])
        arr.push(res)
      }
      // console.log(arr)
      context.commit('getOrderDetailsList', arr)
    }
  }
}
